<?php

	if(isset($_SESSION['members'])){
	if($_SESSION['members']['niveau_id'] >= 2){
	}
	else{
		die('Nee');
	}
}

?>





<html>
<head>
	<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>

<form method="post" action="back_end_gegevens.php">
<input type ="hidden" name="submitted" value="true" />
<label>Search:

<select name="category">
	<option value="naam">Naam</option>
	<option value="postcd">Postcode</option>
	<option value="alles">Alles</option>
</select>
</label>

<label>Search Crit: <input type="text" name="criteria" /></label>

<input type="submit" value="Zoeken" />
</form>





<?php
	if(isset($_POST['submitted'])){
		
		include 'database.php';
		
		$category = $_POST['category'];
		$criteria = $_POST['criteria'];		
		
		
		if($category == 'alles'){
			$query = "SELECT * FROM gegevens";		
			$result = mysqli_query($dbcon, $query) or die('error');			
			
			
			echo "<div id='links'>";
			echo "<table>";
			echo "<tr> <th>Klantid</th> <th>Naam</th> <th>Straat</th> <th>Huisnr</th> <th>postcode</th><th>Delete</th>";
			
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){


				$klant_id = $row['klantid'];
				
				$sql1 = "SELECT rol FROM niveau, members WHERE niveau.id = members.niveau_id AND members.id='$klant_id' ";
				$res = mysqli_query($dbcon, $sql1) or die('error');
				
				while($row2 = mysqli_fetch_array($res, MYSQLI_ASSOC)){
				
					$selectedOption = $row2['rol'];			
					$options = array("", "", "", "");
					
					if($selectedOption == "Klant"){
						$options[0] = "selected='selected'";
					}
					elseif($selectedOption == "Medewerker"){
						$options[1] = "selected='selected'";
					}
					if($selectedOption == "Winkelier"){
						$options[2] = "selected='selected'";
					}
					elseif($selectedOption == "Admin"){
						$options[3] = "selected='selected'";
					}
			
					echo "<tr><td>";
					echo $row['klantid'];
					echo "</td><td>";
					echo $row['naam'];
					echo "</td><td>";
					echo $row['straat'];
					echo "</td><td>";
					echo $row['huisnr'];
					echo "</td><td>";
					echo $row['postcode'];		
					echo "</td><td>";
					echo "<form method='post' action='delete_member.php'>			
					
					<input type='checkbox' name='checkbox[]' id='checkbox[]' value='".$row['klantid']."' />";
					echo "</td><td>";
					echo "<select name='rollen[]'>							
							<option value='Klant' $options[0] >Klant</option>
							<option value='Medewerker' $options[1]>Medewerker</option>
							<option value='Winkelier' $options[2]>Winkelier</option>
							<option value='Admin' $options[3]>Admin</option>
							</select>
							<input type='hidden' name='k_id[]' value='".$row['klantid']."' />
							";
					echo "</td></tr>";
				}
			}			
			echo "</table>";
			
			echo "<input id='delete' type='submit' class='button' name='delete' value='Update'/></form>";
			echo "</div>";			
		}
				
		else{
			$query = "SELECT * FROM gegevens WHERE $category LIKE '%".$criteria."%'  ";
			$result = mysqli_query($dbcon, $query) or die('error');				
			
			echo "<div id='links'>";
			echo "<table>";
			echo "<tr> <th>Klantid</th> <th>Naam</th> <th>Straat</th> <th>Huisnr</th> <th>postcode</th> <th>Delete</th>";
			
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){		
				echo "<tr><td>";
				echo $row['klantid'];
				echo "</td><td>";
				echo $row['naam'];
				echo "</td><td>";
				echo $row['straat'];
				echo "</td><td>";
				echo $row['huisnr'];
				echo "</td><td>";
				echo $row['postcode'];
				echo "</td><td>";
				echo "<form method='post' action='delete_member.php'>			
				
				<input type='checkbox' name='checkbox[]' id='checkbox[]' value='".$row['klantid']."' />";
				echo "</td></tr>";					
			}				
			echo "</table>";
			echo "<input id='delete' type='submit' class='button' name='delete' value='Verwijder Geselecteerden'/></form>";
			echo "</div>";			
		}
	}

?>



</body>
</html>